package com.javajun.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.javajun.custom.DataGridParam;
import com.javajun.custom.EasyuiUtils;
import com.javajun.custom.Success;
import com.javajun.entity.Student;
import com.javajun.form.StudentForm;
import com.javajun.service.StudentServcie;

@Controller
@RequestMapping(value="/student")
public class StudentController {

    @Autowired
    private StudentServcie studentServcie;
    
    @Autowired
    private JavaMailSender jms;
    
    @RequestMapping(value="/manage", method=RequestMethod.GET)
    public String manageView() {
        return "student/manage";
    }
    
    @RequestMapping(value="/search")
    public String searchView(StudentForm form, ModelMap map) {
        map.put("model", form);
        return "student/search";
    }
    /**
     * 表格的请求数据
     * @param param
     * @param form
     * @return
     */
    @RequestMapping(value="/list.do", method=RequestMethod.POST)
    @ResponseBody
    public Object listDo(DataGridParam param, StudentForm form) {
        Pageable pageable = param.buildPageable();
        Specification<Student> spec = studentServcie.buildSpec(form);
        Page<Student> page = studentServcie.findAll(spec, pageable);
        return EasyuiUtils.buildDatagridResult(page);
    }
    
    @RequestMapping(value="/save")
    @ResponseBody
    public Object editDo(StudentForm form) {
        Success success = new Success();
        try {
            studentServcie.save(form);
            success = new Success(true, "保存成功！");
            //建立邮件消息
            SimpleMailMessage mainMessage = new SimpleMailMessage();
            //发送者
            mainMessage.setFrom("admin@javajun.com");
            //接收者
            mainMessage.setTo(form.getEmail());
            //发送的标题
            mainMessage.setSubject("注册成功");
            //发送的内容
            mainMessage.setText("您好，管理员已成功为您在图书管理系统中注册！用户名为您的学号~默认密码为：1111，请您即时登录并修改密码！");
            try {
                jms.send(mainMessage);
            } catch (Exception e) {
                e.printStackTrace();
                success = new Success(true, "数据保存成功，但给用户发送邮件失败哦~");
            }
        } catch (Exception e) {
            // e.printStackTrace();
            success = new Success(false, "出错啦，可能是该邮箱已存在喔~~");
        }
        return success;
    }
    
    @RequestMapping(value="/add")
    public String addView(ModelMap map) {
        Student model = new Student();
        map.put("model", model);
        return "student/add";
    }
    
    @RequestMapping(value="/edit")
    public String editView(Integer id, ModelMap map) {
        Student model = studentServcie.findById(id);
        map.put("model", model);
        return "student/add";
    }
    
    @RequestMapping(value="/delete", method=RequestMethod.POST)
    @ResponseBody
    public Object delete(Integer id) {
        Success success;
        if(id == null) {
            success = new Success(false, "请选择需要删除的数据哦~~");
        } else {
            try {
                studentServcie.deleteById(id);
                success = new Success(true, "您成功删除了一条数据！");
            } catch (Exception e) {
                success = new Success(false, "由于不可抗的因素，您的操作失败了~~");
            }
        }
        
        return success;
    }
    

    
}
